package com.example.smartpark.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.smartpark.common.Result;
import com.example.smartpark.dto.ParkingSpaceAddDTO;
import com.example.smartpark.dto.ParkingSpaceUpdateDTO;
import com.example.smartpark.service.ParkingSpaceService;
import com.example.smartpark.vo.ParkingSpaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 车位管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/parking-spaces")
public class ParkingSpaceController {

    @Autowired
    private ParkingSpaceService parkingSpaceService;

    /**
     * 分页获取车位列表
     *
     * @param page 页码
     * @param size 每页数量
     * @param parkingLotId 停车场ID（可选）
     * @param spaceNumber 车位编号（可选）
     * @param area 所在区域（可选）
     * @param status 状态（可选）
     * @return 分页车位列表
     */
    @GetMapping
    public Result<IPage<ParkingSpaceVO>> getParkingSpacePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer parkingLotId,
            @RequestParam(required = false) String spaceNumber,
            @RequestParam(required = false) String area,
            @RequestParam(required = false) Integer status) {
        log.info("分页获取车位列表: page={}, size={}, parkingLotId={}, spaceNumber={}, area={}, status={}",
                page, size, parkingLotId, spaceNumber, area, status);
        
        IPage<ParkingSpaceVO> parkingSpacePage = parkingSpaceService.getParkingSpacePage(page, size, parkingLotId, spaceNumber, area, status);
        return Result.success(parkingSpacePage);
    }

    /**
     * 获取车位详情
     *
     * @param id 车位ID
     * @return 车位详情
     */
    @GetMapping("/{id}")
    public Result<ParkingSpaceVO> getParkingSpaceById(@PathVariable Integer id) {
        log.info("获取车位详情: id={}", id);
        
        ParkingSpaceVO parkingSpaceVO = parkingSpaceService.getParkingSpaceById(id);
        return Result.success(parkingSpaceVO);
    }

    /**
     * 根据停车场ID获取所有车位
     *
     * @param parkingLotId 停车场ID
     * @return 车位列表
     */
    @GetMapping("/by-lot/{parkingLotId}")
    public Result<List<ParkingSpaceVO>> getParkingSpacesByLotId(@PathVariable Integer parkingLotId) {
        log.info("根据停车场ID获取所有车位: parkingLotId={}", parkingLotId);
        
        List<ParkingSpaceVO> parkingSpaces = parkingSpaceService.getParkingSpacesByLotId(parkingLotId);
        return Result.success(parkingSpaces);
    }

    /**
     * 添加车位
     *
     * @param parkingSpaceAddDTO 添加车位DTO
     * @return 添加的车位信息
     */
    @PostMapping
    public Result<ParkingSpaceVO> addParkingSpace(@RequestBody @Valid ParkingSpaceAddDTO parkingSpaceAddDTO) {
        log.info("添加车位: parkingSpaceAddDTO={}", parkingSpaceAddDTO);
        
        ParkingSpaceVO parkingSpaceVO = parkingSpaceService.addParkingSpace(parkingSpaceAddDTO);
        return Result.success(parkingSpaceVO, "添加车位成功");
    }

    /**
     * 更新车位信息
     *
     * @param id 车位ID
     * @param parkingSpaceUpdateDTO 更新车位DTO
     * @return 更新后的车位信息
     */
    @PutMapping("/{id}")
    public Result<ParkingSpaceVO> updateParkingSpace(
            @PathVariable Integer id,
            @RequestBody @Valid ParkingSpaceUpdateDTO parkingSpaceUpdateDTO) {
        log.info("更新车位: id={}, parkingSpaceUpdateDTO={}", id, parkingSpaceUpdateDTO);
        
        ParkingSpaceVO parkingSpaceVO = parkingSpaceService.updateParkingSpace(id, parkingSpaceUpdateDTO);
        return Result.success(parkingSpaceVO, "更新车位信息成功");
    }

    /**
     * 删除车位
     *
     * @param id 车位ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteParkingSpace(@PathVariable Integer id) {
        log.info("删除车位: id={}", id);
        
        boolean success = parkingSpaceService.deleteParkingSpace(id);
        if (success) {
            return Result.success(null, "删除车位成功");
        } else {
            return Result.error(500, "删除车位失败");
        }
    }

    /**
     * 检查车位编号是否已存在
     *
     * @param parkingLotId 停车场ID
     * @param spaceNumber 车位编号
     * @param excludeId 排除的车位ID（可选，用于更新时）
     * @return 是否存在
     */
    @GetMapping("/check-space-number")
    public Result<Boolean> checkSpaceNumberExists(
            @RequestParam Integer parkingLotId,
            @RequestParam String spaceNumber,
            @RequestParam(required = false) Integer excludeId) {
        log.info("检查车位编号是否已存在: parkingLotId={}, spaceNumber={}, excludeId={}", parkingLotId, spaceNumber, excludeId);
        
        boolean exists = parkingSpaceService.checkSpaceNumberExists(parkingLotId, spaceNumber, excludeId);
        return Result.success(exists);
    }

    /**
     * 批量添加车位
     *
     * @param parkingLotId 停车场ID
     * @param prefix 车位编号前缀
     * @param startNumber 起始编号
     * @param count 车位数量
     * @param spaceType 车位类型
     * @param area 所在区域
     * @param floor 所在楼层
     * @return 添加的车位列表
     */
    @PostMapping("/batch")
    public Result<List<ParkingSpaceVO>> batchAddParkingSpaces(
            @RequestParam Integer parkingLotId,
            @RequestParam String prefix,
            @RequestParam Integer startNumber,
            @RequestParam Integer count,
            @RequestParam Integer spaceType,
            @RequestParam String area,
            @RequestParam String floor) {
        log.info("批量添加车位: parkingLotId={}, prefix={}, startNumber={}, count={}, spaceType={}, area={}, floor={}",
                parkingLotId, prefix, startNumber, count, spaceType, area, floor);
        
        List<ParkingSpaceVO> parkingSpaces = parkingSpaceService.batchAddParkingSpaces(
                parkingLotId, prefix, startNumber, count, spaceType, area, floor);
        return Result.success(parkingSpaces, "批量添加车位成功");
    }
} 